#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;

const int N = 100010;
typedef pair<int, int> PII;

int dist[N];
bool st[N];
vector<vector<PII>> edg;
int n, m;

void spfa()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;

    queue<int> q;
    q.push(1);

    while (q.size())
    {
        int t = q.front(); q.pop();

        st[t] = false;
        for (auto e : edg[t])
        {
            int j = e.first, w = e.second;
            if (dist[j] > dist[t] + w)
            {
                dist[j] = dist[t] + w;
                if (!st[j])
                {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }

    if (dist[n] > 0x3f3f3f3f / 2) puts("impossible");
    else cout << dist[n];
}

int main()
{
    cin >> n >> m;
    edg.resize(n + 1);

    while (m--)
    {
        int x, y, z;
        cin >> x >> y >> z;

        edg[x].push_back({ y, z });
    }

    spfa();

    return 0;
}